package com.cxf.windex.biz;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.entity.BShop;
import com.cxf.windex.entity.JsType;
import com.cxf.windex.mapper.BShopMapper;
import com.github.pagehelper.PageHelper;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @program: business_needs
 * @description: 店铺Biz
 * @author: chenxf
 * @create: 2018-10-11 15:00
 **/
@Service
public class BShopBiz extends BaseBiz<BShopMapper,BShop> {
  @Autowired
  private MoneyBiz moneyBiz;

  /**
   * 店铺列表
   *
   * @param limit 数量
   * @param offset 页码
   * @param orderStr  排序(升序或者降序)
   * @param name 类型名称
   * @return
   */
  public List<BShop> list(int limit, int offset,  String orderStr,String name) {
    Example example = new Example(BShop.class);
    Example.Criteria criteria = example.createCriteria();
    if (StringUtils.isNotBlank(name)) {
      criteria.andLike("name", "%" + name + "%");
    }
    example.setOrderByClause("crt_time " + orderStr);
    PageHelper.startPage(offset, limit);
    return selectByExample(example);
  }

  /**
   * 获取店铺信息
   * @param userId
   * @return
   */
  public BShop getInfo(String userId) {
    BShop entity=new BShop();
    entity.setWxopenid(userId);
    return selectOne(entity);
  }

  /**
   * 保存店铺
   * @param entity
   */
  public void save(BShop entity){
    if(entity.getId()==null||entity.getId()==0){
      insertSelective(entity);
      moneyBiz.initMoney(entity.getWxopenid(),entity.getName());
    }else{
      updateSelectiveById(entity);
    }
  }
}
